package org.linlinjava.litemall.admin.web.order_manage;

import org.linlinjava.litemall.admin.constant.HeaderConstant;
import org.linlinjava.litemall.admin.form.CommodityAddForm;
import org.linlinjava.litemall.admin.form.CommodityAddLinkShopForm;
import org.linlinjava.litemall.admin.form.CommodityDescForm;
import org.linlinjava.litemall.admin.service.order_manage.AgedmallCommodityService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

@RestController
@RequestMapping("/commodity")
@Validated
public class AgedmallCommodityController {

    @Resource
    private AgedmallCommodityService agedmallCommodityService;

    /**
     * 获取商品列表
     */
    @PostMapping("/listCommodity")
    public Object listCommodity(@RequestParam(required = false) String commodityName,
                                @RequestParam(required = false) Integer commdityType,
                                @RequestParam(required = false) Integer commditySubType,
                                @RequestParam(required = false) Integer commdityState,
                                @RequestParam(required = false) String goodsType,
                                @RequestParam(required = false) Integer hasSku,
                                @RequestParam(required = false) Integer storeId,
                                @RequestParam(required = false) Integer isRelate,
                                @RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer limit,
                                HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        return agedmallCommodityService.listCommodity(busiId, commodityName, commdityType, commditySubType, commdityState, goodsType, hasSku, storeId, isRelate, page, limit);
    }

    /**
     * 商品分类
     */
    @PostMapping("/categoryList")
    public Object categoryList(HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        return agedmallCommodityService.categoryList(busiId);
    }

    /**
     * 新增/编辑商品
     */
    @PostMapping("/addCommodity")
    public Object addCommodity(@RequestBody @Validated CommodityAddForm commodityAddForm, HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        return agedmallCommodityService.addCommodity(busiId, commodityAddForm);
    }

    /**
     * 商品详情
     */
    @PostMapping("/commodityDetail")
    public Object commodityDetail(@RequestParam Integer goodsId) {
        return agedmallCommodityService.commodityDetail(goodsId);
    }

    /**
     * 删除商品
     */
    @PostMapping("/delCommodity")
    public Object delCommodity(@RequestParam List<Integer> idList) {
        return agedmallCommodityService.delCommodity(idList);
    }

    /**
     * 更新商品上下架状态
     */
    @PostMapping("/changeState")
    public Object changeState(@RequestParam @NotNull(message = "商品不能为空") @Size(min = 1, message = "商品不能为空") List<Integer> idList,
                              @RequestParam @NotNull(message = "状态不能为空") Integer commodityState) {
        return agedmallCommodityService.changeState(idList, commodityState);
    }

    /**
     * 用于获取商品关联门店的列表
     */
    @PostMapping("/linkShopList")
    public Object linkShopList(@RequestParam Integer type,
                                @RequestParam Integer commodityId,
                                @RequestParam(required = false) Integer province,
                                @RequestParam(required = false) Integer city,
                                @RequestParam(required = false) Integer area,
                                @RequestParam(required = false) String shopKeyWords,
                                @RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer limit, HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        return agedmallCommodityService.linkShopList(busiId, type, commodityId, province, city, area, shopKeyWords, page, limit);
    }

    /**
     * 增加多个该商品的运营门店
     */
    @PostMapping("/linkShops")
    public Object linkShops(@RequestBody CommodityAddLinkShopForm commodityLinkShopsForm) {
        return agedmallCommodityService.linkShops(commodityLinkShopsForm);
    }

    /**
     * 增加编辑单个该商品的运营门店
     */
    @PostMapping("/addLinkShop")
    public Object addLinkShop(@RequestBody CommodityAddLinkShopForm commodityAddLinkShopForm) {
        return agedmallCommodityService.addLinkShop(commodityAddLinkShopForm);
    }

    /**
     * 取消关联该门店
     */
    @PostMapping("/unlinkShop")
    public Object unlinkShop(@RequestParam Long commodityId, @RequestParam List<Long> shopIds) {
        return agedmallCommodityService.unlinkShop(commodityId, shopIds);
    }

    /**
     * 门店商品详情
     */
    @PostMapping("/storeCommodityDetail")
    public Object storeCommodityDetail(@RequestParam Long storeGoodsId) {
        return agedmallCommodityService.storeCommodityDetail(storeGoodsId);
    }

    /**
     * 门店商品详情
     */
    @PostMapping("/storeDetail")
    public Object storeDetail(@RequestParam Long commodityId, @RequestParam Long shopId) {
        return agedmallCommodityService.storeDetail(commodityId, shopId);
    }

    /**
     * 租赁/出售说明设置
     */
    @PostMapping("/setRentDescription")
    public Object setRentDescription(@RequestBody @Validated CommodityDescForm commodityDescForm, HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        return agedmallCommodityService.setRentDescription(busiId, commodityDescForm);
    }

    /**
     * 租赁/出售说明设置
     */
    @PostMapping("/getRentDescription")
    public Object getRentDescription(@RequestParam Integer type, HttpServletRequest request){
        Integer busiId = request.getIntHeader(HeaderConstant.HEADER_BUSIID);
        if (busiId == -1) {
            return ResponseUtil.unauthz();
        }
        return agedmallCommodityService.getRentDescription(busiId, type);
    }

    /**
     * 获取链条、医柜库存
     */
    @PostMapping("/storeGoodsStock")
    public Object storeGoodsStock(@RequestParam Long commodityId, @RequestParam Long storeId) {
        return agedmallCommodityService.storeGoodsStock(commodityId, storeId);
    }

    /**
     * 获取云仓库存
     */
    @PostMapping("/goodsCloudStock")
    public Object goodsCloudStock(@RequestParam Long commodityId) {
        return agedmallCommodityService.goodsCloudStock(commodityId);
    }
}
